<?php
namespace app\index\controller;

use think\Request;

class Shopaddress extends xiaodian{
	
	//零售小店  客户的地址列表
	public function index(){
		
		$param = [
			'action' => 'list',
			'customerid' => CUST_ID
		];

		$response = \ServerHelper::post('shop_address',$param);
		$list = $this->J($response);
		if(!isset($list['data'])){
			$list['data'] = [];
		}
		$onlyselect = Request::instance()->param('onlyselect',0,'intval');
		return view('',['list'=>$list['data'],'onlyselect'=>$onlyselect]);
	}
  
	public function save(){
		if(Request::instance()->isPost()){
			$tsid = Request::instance()->param('tsid',0,'intval');
			$action = Request::instance()->param('action');
			$param = [
				'action' => $action,
				'address' => json_encode([
					'tsid'   => $tsid,
					'customerid' => CUST_ID,
					'consignee' => Request::instance()->param('consignee'), // shou huo ren
					'phonenum' => Request::instance()->param('phonenum'),
					'province' => Request::instance()->param('province'),
					'city' => Request::instance()->param('city'),
					'region' => Request::instance()->param('region'),
					'address' => Request::instance()->param('address'),
					'isdefault' => 0,
				])
			];
			
			$response = \ServerHelper::post('shop_address',$param);
			$data = $this->J($response);
			if(empty($data['flag'])){
				ajax($action == 'add' ? '新增失败' : '保存失败');
			}else{
				ajax($action == 'add' ? '新增成功' : '保存成功',true);
			}
		}else{
			return view();
		}
	}
  
	public function edit(){
		$tsid = Request::instance()->param('tsid');
		if(!\RegexHelper::is_id($tsid)){
			$this->error('参数错误');
		}
		if(Request::instance()->isPost()){
			
			$action = Request::instance()->param('action');
			$param = [
				'action' => 'edit',
				'address' => json_encode([
					'tsid'   => $tsid,
					'customerid' => CUST_ID,
					'consignee' => Request::instance()->param('consignee'), // shou huo ren
					'phonenum' => Request::instance()->param('phonenum'),
					'province' => Request::instance()->param('province'),
					'city' => Request::instance()->param('city'),
					'region' => Request::instance()->param('region'),
					'address' => Request::instance()->param('address'),
					'isdefault' => 0,
				])
			];
			
			$response = \ServerHelper::post('shop_address',$param);
			$data = $this->J($response);
			if(empty($data['flag'])){
				ajax('保存失败');
			}else{
				ajax('保存成功');
			}
		}else{
			$param = [
				'action' => 'list',
				'customerid' => CUST_ID,
				'tsid' =>(int)$tsid
			];

			$response = \ServerHelper::post('shop_address',$param);
			$data = $this->J($response);
			if(empty($data['flag'])){
				ajax('获取失败');
			}
			ajax("SUCCESS",true,$data['data']);
		}
	}
  
	public function delete(){
		$tsid = Request::instance()->param('tsid');
		if(!\RegexHelper::is_id($tsid)){
			$this->error('参数错误');
		}
		$param = [
			'action' => 'delete',
			'customerid' => CUST_ID,
			'tsid' =>(int)$tsid
		];
		$response = \ServerHelper::post('shop_address',$param);
		$data = $this->J($response);
		if(empty($data['flag'])){
			ajax('删除失败');
		}
		ajax('删除成功',true);
    
	}
	public function setdefault(){
		$tsid = Request::instance()->param('tsid');
		if(!\RegexHelper::is_id($tsid)){
			$this->error('参数错误');
		}
		$param = [
			'action' => 'setdefault',
			'customerid' => CUST_ID,
			'tsid' =>(int)$tsid
		];
		$response = \ServerHelper::post('shop_address',$param);
		$data = $this->J($response);
		if(empty($data['flag'])){
			ajax('设置失败');
		}
		ajax('设置成功',true);
	}
  
  
}
